<?php
namespace app\controllers\marketing;
use Yii;
use app\base\Controller;
use app\components\EmailMarket;

class DoDataController extends Controller
{
    public $enableCsrfValidation = false; // 
    // 系统首页
    public function actionIndex()
    { 
        set_time_limit(0);

        $file1 = yii::$app->getRuntimePath()."/bbbb.csv";
        $file = yii::$app->getRuntimePath()."/aaaa.csv";
        
        $data_arr = $data_arr1 = $per_data_arr = array();
        $sql_val =$real_val =  '';
        if(($handle = fopen($file,'r')) !== false)
        {
                while(($data = fgetcsv($handle)) !== false)
                {
                        if($user_id = intval($data[0]))
                        {
                            $sql_val  .= '('.$user_id.'),';
                        }
                        

                }

        }
        fclose($handle);
        $sql = "insert into trim_data(user_id) values".substr($sql_val, 0,-1);
        Yii::$app->db->createCommand($sql)->execute();
        if(($handle = fopen($file1,'r')) !== false)
        {
                while(($data = fgetcsv($handle)) !== false)
                {
                    if($user_id = intval($data[0]))
                    {
                        $real_val .= '('.$user_id.'),';
                    }
                }

        }
        fclose($handle);
        $sql = "insert into real_data(user_id) values".substr($real_val, 0,-1);
        Yii::$app->db->createCommand($sql)->execute();
        echo 'done';
    }
  
    public function actionTestSynUsers()
    {
        //$apiUrl = 'http://ems.appinthestore.com/api-user/syn-user?module_name=marketing_email';
        $apiUrl = "http://{$_SERVER['HTTP_HOST']}/api-user/syn-user?module_name=marketing_email";
        $postArr = [
            "token" => '7f16ef1595084a43269279221d99bb2a',
            "site_name" => 'dresslily.com',
            "data" => '%5B%7B%22login_device_type%22%3A%221%22%2C%22user_rank%22%3A%220%22%2C%22user_id%22%3A%227893995%22%2C%22email%22%3A%22btGX9by8a1WdV3zXLJmUNHyRYKWkZ70ec900BNneb2W7Fcp0btCb5yjLbB2u0U%3DL%22%2C%22subscribe_state%22%3A1%2C%22subscribe_state_update_time%22%3A%221502876403%22%2C%22first_name%22%3A%22%22%2C%22last_login%22%3A%221504031804%22%2C%22reg_time%22%3A%221502876402%22%2C%22ip%22%3A%222.86.118.32%22%2C%22last_order_time%22%3A%221502876597%22%2C%22lang%22%3A%22en%22%2C%22sex%22%3A%220%22%2C%22birthday%22%3A%22%22%2C%22country%22%3A%22GR%22%7D%2C%7B%22login_device_type%22%3A%221%22%2C%22user_rank%22%3A%220%22%2C%22user_id%22%3A%227894001%22%2C%22email%22%3A%22btGX9by8a1WdV3zXLJmUNHyRYKWkZ70ec900BNneb2W7Fcp0btCb5yjLbB2u0U%3DL%22%2C%22subscribe_state%22%3A1%2C%22subscribe_state_update_time%22%3A%221502876501%22%2C%22first_name%22%3A%22%22%2C%22last_login%22%3A%221504079678%22%2C%22reg_time%22%3A%221502876501%22%2C%22ip%22%3A%22188.250.121.37%22%2C%22last_order_time%22%3A%221502876730%22%2C%22lang%22%3A%22en%22%2C%22sex%22%3A%220%22%2C%22birthday%22%3A%22%22%2C%22country%22%3A%22PT%22%7D%2C%7B%22login_device_type%22%3A%222%22%2C%22user_rank%22%3A%220%22%2C%22user_id%22%3A%227894183%22%2C%22email%22%3A%22btGX9by8a1WdV3zXLJmUNHyRYKWkZ70ec900BNneb2W7Fcp0btCb5yjLbB2u0U%3DL%22%2C%22subscribe_state%22%3A0%2C%22first_name%22%3A%22%22%2C%22last_login%22%3A%221503967858%22%2C%22reg_time%22%3A%221502881030%22%2C%22ip%22%3A%2273.30.55.158%22%2C%22last_order_time%22%3A%221503009792%22%2C%22lang%22%3A%22en%22%2C%22sex%22%3A%220%22%2C%22birthday%22%3A%22%22%2C%22country%22%3A%22US%22%7D%2C%7B%22login_device_type%22%3A%224%22%2C%22user_rank%22%3A%220%22%2C%22user_id%22%3A%227894635%22%2C%22email%22%3A%22dtGXlbj8a1GdF3fXcJHUNHzRcK0kB7oeb930RNteY2W7lcs0LtmbNyvLbBQu%3DU%3DL%22%2C%22subscribe_state%22%3A1%2C%22subscribe_state_update_time%22%3A%221502888758%22%2C%22first_name%22%3A%22%22%2C%22last_login%22%3A%221503224308%22%2C%22reg_time%22%3A%221502888758%22%2C%22ip%22%3A%22106.205.68.53%22%2C%22last_order_time%22%3A%221503224397%22%2C%22lang%22%3A%22en%22%2C%22sex%22%3A%220%22%2C%22birthday%22%3A%22%22%2C%22country%22%3A%22IN%22%7D%2C%7B%22login_device_type%22%3A%221%22%2C%22user_rank%22%3A%220%22%2C%22user_id%22%3A%227895004%22%2C%22email%22%3A%22dtGXlbj8a1GdF3fXcJHUNHzRcK0kB7oeb930RNteY2W7lcs0LtmbNyvLbBQu%3DU%3DL%22%2C%22subscribe_state%22%3A0%2C%22first_name%22%3A%22MELISSA%22%2C%22last_login%22%3A%221503977743%22%2C%22reg_time%22%3A%221502893486%22%2C%22ip%22%3A%22172.72.107.112%22%2C%22last_order_time%22%3A%221502893537%22%2C%22lang%22%3A%22en%22%2C%22sex%22%3A%220%22%2C%22birthday%22%3A%22%22%2C%22country%22%3A%22US%22%7D%2C%7B%22login_device_type%22%3A%221%22%2C%22user_rank%22%3A%220%22%2C%22user_id%22%3A%227895078%22%2C%22email%22%3A%22btGXlbs8b1Gdl3hXbJmUJH1RcK2kh7AeY9m0xN1eZ2X7lcv0btmbRylLcBiu5UjLb0y351aw%3D%3D%22%2C%22subscribe_state%22%3A0%2C%22first_name%22%3A%22user7895078%22%2C%22last_login%22%3A%221502914167%22%2C%22reg_time%22%3A%221502894275%22%2C%22ip%22%3A%2282.26.206.84%22%2C%22last_order_time%22%3A%221502915166%22%2C%22lang%22%3A%22en%22%2C%22sex%22%3A%220%22%2C%22birthday%22%3A%22%22%2C%22country%22%3A%22US%22%7D%2C%7B%22login_device_type%22%3A%222%22%2C%22user_rank%22%3A%220%22%2C%22user_id%22%3A%227895231%22%2C%22email%22%3A%22YtiX5bt8Y1XdR3zXaJGUlHkRaKXkN7veQ9H0lNhea2G79cv0LtmbNyvLbBQu%3DU%3DL%22%2C%22subscribe_state%22%3A1%2C%22subscribe_state_update_time%22%3A%221502895896%22%2C%22first_name%22%3A%22%22%2C%22last_login%22%3A%221503230073%22%2C%22reg_time%22%3A%221502895896%22%2C%22ip%22%3A%2241.113.158.194%22%2C%22last_order_time%22%3A%22%22%2C%22lang%22%3A%22en%22%2C%22sex%22%3A%220%22%2C%22birthday%22%3A%22%22%2C%22country%22%3A%22ZA%22%7D%2C%7B%22login_device_type%22%3A%221%22%2C%22user_rank%22%3A%220%22%2C%22user_id%22%3A%227895236%22%2C%22email%22%3A%22btGX9by8Z1Wd93sXZJUUBHoRbK3kR7teY9W0lNseL2m7Zcy0%22%2C%22subscribe_state%22%3A0%2C%22first_name%22%3A%22Laura%22%2C%22last_login%22%3A%221503497264%22%2C%22reg_time%22%3A%221502895957%22%2C%22ip%22%3A%2292.170.184.91%22%2C%22last_order_time%22%3A%221502896096%22%2C%22lang%22%3A%22en%22%2C%22sex%22%3A%220%22%2C%22birthday%22%3A%22%22%2C%22country%22%3A%22FR%22%7D%2C%7B%22login_device_type%22%3A%221%22%2C%22user_rank%22%3A%220%22%2C%22user_id%22%3A%227895303%22%2C%22email%22%3A%22atGXFbu8a12dl3uXcJ2U9HuRcKGkF7teM9j0ZNAeZ2271ch0atWbwyuLYB2u9UtL%22%2C%22subscribe_state%22%3A0%2C%22first_name%22%3A%22hankinson%22%2C%22last_login%22%3A%221503947917%22%2C%22reg_time%22%3A%221502896733%22%2C%22ip%22%3A%2286.163.212.191%22%2C%22last_order_time%22%3A%221502897240%22%2C%22lang%22%3A%22en%22%2C%22sex%22%3A%220%22%2C%22birthday%22%3A%22%22%2C%22country%22%3A%22GB%22%7D%2C%7B%22login_device_type%22%3A%221%22%2C%22user_rank%22%3A%220%22%2C%22user_id%22%3A%227895303%22%2C%22email%22%3A%22atGXFbu8a12dl3uXcJ2U9HuRcKGkF7teM9j0ZNAeZ2271ch0atWbwyuLYB2u9UtL%22%2C%22subscribe_state%22%3A0%2C%22first_name%22%3A%22hankinson%22%2C%22last_login%22%3A%221503947917%22%2C%22reg_time%22%3A%221502896733%22%2C%22ip%22%3A%2286.163.212.191%22%2C%22last_order_time%22%3A%221502897240%22%2C%22lang%22%3A%22en%22%2C%22sex%22%3A%220%22%2C%22birthday%22%3A%22%22%2C%22country%22%3A%22GB%22%7D%5D'
                ];
//        $emailObj = new EmailMarket();
//        $email = $emailObj->decryption('##XQ38MxsO9Vl+Ydi+LGaH7WAQ/tVK/eBLKPjtsabkOBw=');
//        $email = $emailObj->transferDecryption('MtjXAbw8M1TdE3zXOJTURHARcK3kR71eZ9G0VNued2H7Mcu0ctGblyuLYB2utUuLZ0X3lwaXJhdGVz');
//        var_dump($email);exit;
//        $apiToken = yii::$app->params['marketingApiToken'];
//        $token = md5($apiToken . $postArr['data']);
//        var_dump($token);exit;
//       $data = json_decode(urldecode($postArr['data']), true);
//       $data[0]['email'] = 'btGX9by8a1WdV3zXLJmUNHyRYKWkZ70ec900BNneb2W7Fcp0btCb5yjLbB2u0U=L';
//       $data[1]['email'] = 'btGX9by8a1WdV3zXLJmUNHyRYKWkZ70ec900BNneb2W7Fcp0btCb5yjLbB2u0U=L';
//       $data[2]['email'] = 'btGX9by8a1WdV3zXLJmUNHyRYKWkZ70ec900BNneb2W7Fcp0btCb5yjLbB2u0U=L';
//       $data[3]['email'] = 'dtGXlbj8a1GdF3fXcJHUNHzRcK0kB7oeb930RNteY2W7lcs0LtmbNyvLbBQu=U=L';
//       $data[4]['email'] = 'dtGXlbj8a1GdF3fXcJHUNHzRcK0kB7oeb930RNteY2W7lcs0LtmbNyvLbBQu=U=L';
//       $datas = array_slice($data,0,10);
//       var_dump(urlencode(json_encode($datas)));exit;
        // 使用curl发送用户数据
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $apiUrl);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postArr);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 1000);
        $result = curl_exec($ch); //返回结果, json格式
        echo "<pre>";
        print_r($result);
        echo "</pre>";
        //var_dump($result);
        curl_close($ch);
        
    }

    
    
}
